<?php

class paginator
{
    private static $_pages = array();
    private static $_items_on_page = array(0=>10);
    private static $_current_page = array(0=>1);


    /**
     * Set Current page number
     *
     * @param integer $page
     * @param string $namespace
     */
    public static function setCurrentPage($page, $namespace = 0)
    {
       if(is_numeric($page))
         self::$_current_page[$namespace] = $page;
    }


    /**
     * Get Current page number
     * @return integer
     */
    public static function getCurrentPage($namespace = 0)
    {
       return self::$_current_page[$namespace];
    }



    /**
     * Set Items on page
     *
     * @param integer $items
     */
    public static function setItemsOnPage($items, $namespace = 0)
    {
       if(is_numeric($items))
          self::$_items_on_page[$namespace] = $items;
    }
    

    /**
     * Get Items on page
     * 
     * @return integer
     */
    public static function getItemsOnPage($namespace = 0)
    {
       return self::$_items_on_page[$namespace];
    }


    public static function setPages($found, $namespace = 0)
    {
       if(empty($found))
       {
          self::$_pages[$namespace] = FALSE;
          return FALSE;
       }

       $pages_count = ceil($found / self::getItemsOnPage($namespace));
       if($pages_count == 1)
       {
          self::$_pages[$namespace] = FALSE;
          return FALSE;
       }

       $lang    = request::getLang('current');
       $url     = request::getLang('defaultId') != $lang['id'] && config::read('langs_domain') == FALSE ? '/'. $lang['alias'] : '';
       $url    .= request::getRequest() ? '/'. implode('/', request::getRequest()) .'/' : '/';

       $pages = array();
       $pages['found']  = $found;
       $pages['onpage'] = self::getItemsOnPage($namespace);
       $pages['pages']  = ceil($found / self::getItemsOnPage($namespace));
       
       for($i=1; $i<=$pages_count; $i++)
       {
          # SET EMPTY PLACEHOLDER
          if(($i > 2 && ($i+3) < self::getCurrentPage($namespace)) || (self::getCurrentPage($namespace) < ($i-3) && $i < ($pages_count - 1)))
          {
             $lastCountedPage = end($pages['links']);
             if(!$lastCountedPage['gap'])
             {
                $tmp            = array();
                $tmp['num']     = '...';
                $tmp['gap']     = true;
                $tmp['current'] = false;
                $tmp['alias']   = '';
                $tmp['url']     = '';
                $tmp['href']    = '';
                
                $pages['links'][] = $tmp;
             }
             continue;
          }

          $tmp            = array();
          $tmp['num']     = $i;
          $tmp['gap']     = false;
          $tmp['current'] = $i == self::getCurrentPage($namespace) ? true : false;
          $tmp['alias']   = 'page'. $i;
          $tmp['url']     = $url .'page'. $i .'/';
          $tmp['href']    = $url .'page'. $i .'.'. config::read('url_extention');

          if($i == self::getCurrentPage($namespace))
          {
             $pages['show'] = $i == $pages_count ? $found%$pages_count : self::getItemsOnPage($namespace);
          }
          
          if(($i-1) == self::getCurrentPage($namespace))
             $pages['next'] = $tmp;

          if(($i+1) == self::getCurrentPage($namespace))
             $pages['prev'] = $tmp;

          $pages['links'][] = $tmp;
       }

       if(self::getCurrentPage($namespace) == 1)
          $pages['prev'] = FALSE;

       if(self::getCurrentPage($namespace) == $pages_count && $pages_count > 1)
          $pages['next'] = FALSE;


       self::$_pages[$namespace] = $pages;
       return $pages;
    }


    /**
     * Get Array of Pages
     *
     * @param string $namespace
     * @return array
     */
    public static function getPages($namespace = 0)
    {
       if(isset(self::$_pages[$namespace]))
          return self::$_pages[$namespace];
       return FALSE;
    }
}


//TODO: если установить пространство именн, то это пространство как то должно попадать внутрь к примеру класса статей, а то там вызывается билд постраничных ссылок